/**
 * Created by zhaojm on 15/3/28.
 */
// 飞船
game.Ship = game.RoleProtocol.extend({

    _canBeAttack : null,
    _action : null,

    _ghostSprite : null,
    _HP : null,

    _layer : null,
    _delayTime : null,
    _delayTimeCount : null,

    ctor:function(){
        this._super();
        var size = cc.winSize;

        this._canBeAttack = false;
        this._delayTime = 0.16;
        this._delayTimeCount = 0;

        this._HP = game.Config.ship.HP;

        var frame0 = new cc.SpriteFrame(res.ship01_png, cc.rect(0, 0, 60, 38));
        var frame1 = new cc.SpriteFrame(res.ship01_png, cc.rect(60, 0, 60, 38));

        this._sprite = new cc.Sprite(frame0);

        this._action = new cc.RepeatForever(new cc.Animate(new cc.Animation([frame0, frame1], 0.1)));


        this._ghostSprite = new cc.Sprite(res.ship01_png, cc.rect(0, 45, 60, 38));
        this._ghostSprite.setBlendFunc(gl.SRC_ALPHA, gl.ONE);
        this._ghostSprite.setScale(8);
        this._ghostSprite.setPosition(cc.p(this._sprite.getContentSize().width / 2, 12));
        this._sprite.addChild(this._ghostSprite, 3000, 99999);

        this._sprite.setPosition(cc.p(size.width * 0.5, size.height * 0.1));

    },

    addToLayer : function(layer){
        this._layer = layer;

        layer.addChild(this._sprite);

        this._sprite.runAction(this._action);

        this._ghostSprite.runAction(new cc.ScaleTo(0.5, 1, 1));

        var blinks = new cc.Blink(3, 9);
        var makeBeAttack = new cc.CallFunc(this.makeAttack);

        this._sprite.runAction(new cc.Sequence(new cc.DelayTime(0.5), blinks, makeBeAttack));

    },

    removeFromLayer : function(){
        this._sprite.removeFromParent();
        game.Globals.ship = this._layer._ship = null;
    },

    destroy : function(){

        var explode = new game.ExplodeEffect();
        explode.addToLayer(this._layer, this._sprite.getPosition());

        this.removeFromLayer();

        game.Config.incLifeCount();

// 音效
        //if (Config::sharedConfig()->getAudioState()){
        //    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(s_shipDestroyEffect);
        //
        //}
    },

    hurt : function(){
        if (this._canBeAttack) {
            this._HP--;
            this._sprite.setColor(new cc.Color(255, 0, 0));
        }
    },
    collideRect : function(){
        var pos = this._sprite.getPosition();
        var cs = this._sprite.getContentSize();

        return new cc.rect(pos.x - cs.width / 2 , pos.y - cs.height / 2, cs.width, cs.height / 2);
    },

    shoot : function(){
        var self = this;
        var offset = 13;
        var position = self._sprite.getPosition();
        var contentSize = self._sprite.getContentSize();

        var bullet_a = game.Bullet.createShipBullet();
        bullet_a.addToLayer(self._layer);
        self._layer.play_bullet.push(bullet_a);
        bullet_a.setPosition(cc.p(position.x + offset, position.y + 3 + contentSize.height * 0.3));

        var bullet_b = game.Bullet.createShipBullet();
        bullet_b.addToLayer(self._layer);
        self._layer.play_bullet.push(bullet_b);
        bullet_b.setPosition(cc.p(position.x - offset, position.y + 3 + contentSize.height * 0.3));

    },

    makeAttack:function(pSender){
        var self = game.Globals.ship;
        self._canBeAttack = true;
        self._sprite.setVisible(true);
        pSender.removeChildByTag(99999);
    },

    update:function(dt){
        if (this._HP <= 0) {
            this.destroy();
            return;
        }

        this._delayTimeCount += dt;
        if(this._delayTimeCount >= this._delayTime){
            this._delayTimeCount = 0;
            this.shoot();
        }

    },

    getPosition:function(){
        return this._sprite.getPosition();
    },
    setPosition:function(pos){
        this._sprite.setPosition(pos);
    },

});